﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using YF_Music.Core.Models;
using YF_Music.Core.Converters;
using System.Text.Encodings.Web;

namespace YF_Music.Core.Caches
{
  public static class PlaylistManager
  {
    private static readonly string PlaylistDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache", "playlists");

    private static readonly JsonSerializerOptions s_writeOptions = new()
    {
      WriteIndented = true,
      Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
    };
    public static void SavePlaylist(string name, List<MusicItem> items)
    {
      Directory.CreateDirectory(PlaylistDir);
      string path = Path.Combine(PlaylistDir, $"{name}.json");

      var list = items.Select(MusicItemConverter.ToSerializable).ToList();
      var json = JsonSerializer.Serialize(list, s_writeOptions);
      File.WriteAllText(path, json);
    }

    public static List<MusicItem> LoadPlaylist(string name)
    {
      string path = Path.Combine(PlaylistDir, $"{name}.json");
      if (!File.Exists(path)) return [];

      var json = File.ReadAllText(path);
      var list = JsonSerializer.Deserialize<List<SerializableMusicItem>>(json);
      return list?.Select(MusicItemConverter.FromSerializable).ToList() ?? [];
    }

    public static List<string> GetAllPlaylistNames()
    {
      if (!Directory.Exists(PlaylistDir)) return [];

      return Directory.GetFiles(PlaylistDir, "*.json")
          .Select(file => Path.GetFileNameWithoutExtension(file))
          .ToList();
    }

    public static void DeletePlaylist(string name)
    {
      string path = Path.Combine(PlaylistDir, $"{name}.json");
      if (File.Exists(path)) File.Delete(path);
    }
  }
  //public static class MusicCacheManager
  //{
  //  private static readonly string CacheFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache");
  //  private static readonly string ArtFolder = Path.Combine(CacheFolder, "album_art");
  //  private static readonly string CacheFile = Path.Combine(CacheFolder, "music_cache.json");

  //  static MusicCacheManager()
  //  {
  //    Directory.CreateDirectory(CacheFolder);
  //    Directory.CreateDirectory(ArtFolder);
  //  }
  //  private static readonly JsonSerializerOptions s_writeOptions = new()
  //  {
  //    WriteIndented = true
  //  };

  //  public static void Save(List<MusicItem> items)
  //  {
  //    var serializableList = new List<SerializableMusicItem>();

  //    foreach (var item in items)
  //    {
  //      string imagePath = SaveAlbumArtToFile(item.AlbumArt, item.ID);
  //      serializableList.Add(item.ToSerializable(imagePath));
  //    }

  //    var json = JsonSerializer.Serialize(serializableList, s_writeOptions);

  //    File.WriteAllText(CacheFile, json);
  //  }

  //  public static List<MusicItem> Load()
  //  {
  //    if (!File.Exists(CacheFile)) return new List<MusicItem>();

  //    var json = File.ReadAllText(CacheFile);
  //    var list = JsonSerializer.Deserialize<List<SerializableMusicItem>>(json);
  //    return list.Select(MusicItem.FromSerializable).ToList();
  //  }

  //  private static string SaveAlbumArtToFile(ImageSource image, string id)
  //  {
  //    if (image is not BitmapSource bitmap) return null;

  //    string path = Path.Combine(ArtFolder, $"{id}.png");

  //    var encoder = new PngBitmapEncoder();
  //    encoder.Frames.Add(BitmapFrame.Create(bitmap));

  //    using var fs = new FileStream(path, FileMode.Create);
  //    encoder.Save(fs);

  //    return path;
  //  }

  //  private static ImageSource LoadAlbumArtFromFile(string path)
  //  {
  //    if (!File.Exists(path))
  //    {
  //      return MusicItem.LoadDefaultImage();
  //    }

  //    var bitmap = new BitmapImage();
  //    bitmap.BeginInit();
  //    bitmap.UriSource = new Uri(path, UriKind.Absolute);
  //    bitmap.CacheOption = BitmapCacheOption.OnLoad;
  //    bitmap.EndInit();
  //    bitmap.Freeze();
  //    return bitmap;
  //  }
  //}

}
